package com.hs.carrental.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.carrental.dto.store.StoreDeleteDto;
import com.hs.carrental.dto.store.StorePageDto;
import com.hs.carrental.dto.store.StoreSaveDto;
import com.hs.carrental.entity.Admin;
import com.hs.carrental.entity.Car;
import com.hs.carrental.entity.Store;
import com.hs.carrental.mapper.AdminMapper;
import com.hs.carrental.mapper.CarMapper;
import com.hs.carrental.mapper.StoreMapper;
import com.hs.carrental.service.IStoreService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hs.carrental.util.Helper;
import com.hs.carrental.vo.store.StorePageVo;
import com.hs.carrental.vo.store.StoreVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author lyy
 * @since 2021-03-17
 */
@Service
public class StoreServiceImpl extends ServiceImpl<StoreMapper, Store> implements IStoreService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private AdminMapper adminMapper;

    @Autowired
    private CarMapper carMapper;

    @Override
    public List<StoreVo> getStoreEnum() {
        return storeMapper.getStoreEnum();
    }

    /**
     * 新增或编辑
     * @param saveDto
     * @return
     */
    @Override
    public boolean save(StoreSaveDto saveDto) {
        boolean save = false;
        Store store = new Store();
        BeanUtils.copyProperties(saveDto, store);
        if (Helper.isEmpty(saveDto.getStoreId())) {
            //新增
            save = this.save(store);
        } else {
            //编辑
            save = this.updateById(store);
        }
        return save;
    }

    /**
     * 分页
     * @param storePageDto
     * @return
     */
    @Override
    public IPage<StorePageVo> getPage(StorePageDto storePageDto) {
        //新建分页对象
        Page<Store> page = new Page<>(storePageDto.getPageNum(), storePageDto.getPageSize());
        //分页查询
        IPage<StorePageVo> pageVoIPage = storeMapper.getPage(page, storePageDto.getStore(), storePageDto.getShopOwner());
        return pageVoIPage;
    }

    /**
     * 删除
     * @param storeDeleteDto
     * @return
     */
    @Override
    public boolean delete(StoreDeleteDto storeDeleteDto) {
        boolean remove = false;
        //删除门店信息
        remove = this.removeById(storeDeleteDto.getId());
        UpdateWrapper<Admin> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("storeId", storeDeleteDto.getId());
        //删除该门店管理员
        adminMapper.delete(updateWrapper);

        UpdateWrapper<Car> carUpdateWrapper = new UpdateWrapper<>();
        carUpdateWrapper.eq("storeId", storeDeleteDto.getId());
        //删除该门店下车辆信息
        carMapper.delete(carUpdateWrapper);
        return remove;
    }
}
